<template>
  <div class='top-category'>
    <div class="container">
        <!-- 面包屑导航 -->
      <xtx-bread seperator='>'>
        <xtx-breadItem to='/'>首页</xtx-breadItem>
        <xtx-breadItem >{{categoryName}}</xtx-breadItem>
      </xtx-bread>
       <!-- 轮播图 -->
      <XtxSlider :sliders="sliders" />
      <!-- 分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in categoryList" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" >
              <p> {{i.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in categoryList" :key="item.id">
          <div class="head">
            <h3>-  {{item.name}}  -</h3>
          </div>
          <div class="body">
            <RouterLink to="/" class="goods-item" v-for="good in item.goods" :key="good.id">
              <img
                :src="good.picture"
                alt=""
              />
              <p class="name ellipsis"> {{good.name}} </p>
              <p class="desc ellipsis"> {{good.desc}} </p>
              <p class="price">&yen;{{good.price}} </p>
            </RouterLink>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findBanner } from '@/api/home'
import { ref, onMounted } from 'vue'
import { findTopCategory } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  name: 'TopCategory',
  setup () {
    // banner功能
    const sliders = ref([])
    async function loadBanner () {
      const { result } = await findBanner()
      sliders.value = result
    }
    onMounted(() => {
      loadBanner()
    })

    // 轮播图功能
    const categoryList = ref([])
    // 面包屑导航名字
    const categoryName = ref('')
    const route = useRoute()
    async function loadCategoryList () {
      const res = await findTopCategory(route.params.id)
      console.log(res)
      categoryList.value = res.result.children
      categoryName.value = res.result.name
    }
    onMounted(() => {
      loadCategoryList()
    })
    // onBeforeRouteUpdate((to) => {
    //   console.log('路由更新了')
    //   // 在这里重新调用接口就行了
    //   loadCategoryList(to.params.id)
    // })
    return {
      sliders,
      categoryList,
      categoryName
    }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: 400;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
  }
  .sub-list ul {
    display: flex;
    padding: 0 32px;
    flex-wrap: wrap;
    min-height: 160px;
    li {
      width: 168px;
      height: 160px;
      a {
        text-align: center;
        display: block;
        font-size: 16px;
        img {
          width: 100px;
          height: 100px;
        }
        p {
          line-height: 40px;
        }
      }
    }
  }
}
.ref-goods {
  margin-top:20px;
  background-color: #fff;
  .head {
    position: relative;
    .more {
      position: absolute;
      right: 20px;
      top: 30px;
    }
  }
  .body {
    display: flex;
    justify-content: space-around;
    padding: 0 20px;
    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        padding-top: 10px;
      }
      .name {
        font-size: 16px;
      }
      .desc {
        color: #999;
        height: 29px;
      }
      .price {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
</style>
